package com.it.controller;

import com.it.entity.Log;
import com.it.entity.User;
import com.it.service.LogService;
import com.it.service.UserService;
import com.it.tools.AjaxException;
import com.it.tools.CosFileupload;
import com.it.tools.JsonResult;
import com.it.tools.NormalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class LogColler {

    @Autowired
    UserService uservice;

    @Autowired
    LogService logs;

    /**
     * 查询
     * @param uid
     * @param lotion
     * @return
     */
    @RequestMapping(path= "/life/{uid}/{lotion}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getsea(@PathVariable("uid") int uid, @PathVariable("lotion") int lotion) throws AjaxException {
        try{
            Log lo=new Log();
            //用户ID
            lo.setUsid(uid);
            //位置
            lo.setLocation(lotion);
            //查询
            List lis=logs.seconfig(lo);
            JsonResult js=new JsonResult(200,"成功！",lis);
            return js;
        }catch(Exception e){
            throw new AjaxException(e.getMessage());
        }
    }

    /**
     * 删除
     * @param loid
     * @return
     */
    @RequestMapping(path = "/life/{loid}",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult del(@PathVariable("loid") int loid) throws AjaxException {
       try {
           //执行删除
           int cf=logs.del(loid);
           JsonResult js=new JsonResult();
           //判断是否删除
           if(cf>0){
               //成功删除
               js=new JsonResult(200,"成功！");
           }else{
               //删除失败
               js=new JsonResult(500,"失败！");
           }
           return js;
       }catch (Exception e){
           throw new AjaxException(e.getMessage());
       }
    }

    /**
     * 添加
     * @param log
     * @return
     */
    @RequestMapping(path = "/life",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult add(Log log) throws AjaxException {
        System.out.println("log:"+log);
       try {
           JsonResult js=new JsonResult();
           //执行添加
           int ad=logs.addlog(log);
           //判断是否添加成功
           if(ad>0){
               //成功
               js=new JsonResult(200,"成功！",log.getLocation());
           }else{
               //错误
               js=new JsonResult(500,"失败！");
           }
           return js;
       }catch (Exception e){
           throw new AjaxException(e.getMessage());
       }
    }

    /**
     * ajax文件
     * @return
     * @throws AjaxException
     */
    @RequestMapping("/fileup")
    @ResponseBody
    public JsonResult filup(HttpServletRequest request, MultipartFile[] multipart) throws AjaxException, NormalException {
        JsonResult js=new JsonResult();
        for(MultipartFile file : multipart){
            //文件name
            String fname=file.getOriginalFilename();
            //得到时间戳
            Long time=System.currentTimeMillis();
            //找到.
            int wz=fname.lastIndexOf(".");
            //后缀名
            String filna=fname.substring(wz+1);
            System.out.println("后缀名："+filna);
            //图片名字设置时间戳
            String pna=time+"."+filna;
            System.out.println("图片名："+pna);
            try {
                CosFileupload.upfile(file.getInputStream(),pna);
                String imgurl="https://sls-study-cloud-1301165591.cos.ap-guangzhou.myqcloud.com/"+pna;
                User us=new User();
                int id= (int) request.getSession().getAttribute("uid");
                us.setId(id);
                us.setImg(imgurl);
                int up=uservice.UserUpda(us);
                if(up>0){
                    js=new JsonResult(200,"成功！",imgurl);
                }else{
                    js=new JsonResult(500,"失败！");
                }
            }catch (Exception e) {
                throw new NormalException(e.getMessage());
            }
        }
        return js;
    }

}
